/*
Rilego - An easy Images 2 E-Book converter.
Copyright (C) 2011  Federico Improta (Kirys)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */
package it.kirys.rilego.gui.filtersmanager.nodesmanaging;

import it.kirys.rilego.engine.processors.imagefilters.BrightessContrastFilter;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.HashMap;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

/**
 *
 ** @author Federico Improta
 */
public class LeafRenderer implements TreeCellRenderer {

	Color selectionBorderColor, selectionForeground, selectionBackground,
			  textForeground, textBackground;

	public LeafRenderer() {
		Font fontValue;
		fontValue = UIManager.getFont("Tree.font");
		/*		if (fontValue != null) {
		leafRenderer.setFont(fontValue);
		}*/
		/*		Boolean booleanValue = (Boolean) UIManager.get("Tree.drawsFocusBorderAroundIcon");
		leafRenderer.setFocusPainted((booleanValue != null)
		&& (booleanValue.booleanValue()));*/

		selectionBorderColor = UIManager.getColor("Tree.selectionBorderColor");
		selectionForeground = UIManager.getColor("Tree.selectionForeground");
		selectionBackground = UIManager.getColor("Tree.selectionBackground");
		textForeground = UIManager.getColor("Tree.textForeground");
		textBackground = UIManager.getColor("Tree.textBackground");


	}

	
	JComponent getRenderer(boolean leaf, Object value) {

		//we have to generate a renderer
		if (value instanceof IEditableTreeLeaf) {
			//we have to generate the proper editor
			IEditableTreeLeaf etl= (IEditableTreeLeaf)value;
			return etl.getEditor();
		} else {
			return new DefaultTreeCellRenderer();
		}

	}//JComponent getRenderer(boolean leaf, Object value)

	@Override
	public Component getTreeCellRendererComponent(JTree tree, Object value,
			  boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
		JComponent renderer=getRenderer(leaf, value);
		if (renderer instanceof IVisualLeafEditor) {
			//renderer.setEnabled(true);
			//it is one of the editor
			//we have to set some colors and styles
			renderer.setEnabled(tree.isEnabled());
			if (selected) {
				renderer.setForeground(selectionForeground);
				renderer.setBackground(selectionBackground);
			} else {
				renderer.setForeground(textForeground);
				renderer.setBackground(textBackground);
			}
			//now we have to set the value
			IVisualLeafEditor editor = (IVisualLeafEditor) renderer;
			if (value instanceof ITreeLeaf){
				ITreeLeaf leafobj=(ITreeLeaf)value;
				editor.setLabel(leafobj.getLabel());
				if (leafobj instanceof IEditableTreeLeaf){
					editor.setValue(((IEditableTreeLeaf)leafobj).getValue());
				}
			}
			return renderer;
		} else if (renderer instanceof DefaultTreeCellRenderer) {
			//it is the stanard three renderer
			DefaultTreeCellRenderer dtcr = (DefaultTreeCellRenderer) renderer;
			return dtcr.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
		} else {
			//it is something else
			return renderer;
		}

		/*		if (leaf) {

		String stringValue = tree.convertValueToText(value, selected,
		expanded, leaf, row, false);
		leafRenderer.setText(stringValue);
		leafRenderer.setSelected(false);

		leafRenderer.setEnabled(tree.isEnabled());

		if (selected) {
		leafRenderer.setForeground(selectionForeground);
		leafRenderer.setBackground(selectionBackground);
		} else {
		leafRenderer.setForeground(textForeground);
		leafRenderer.setBackground(textBackground);
		}

		if ((value != null) && (value instanceof DefaultMutableTreeNode)) {
		Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
		if (userObject instanceof BooleanLeaf) {
		BooleanLeaf node = (BooleanLeaf) userObject;
		leafRenderer.setText(node.getLabel());
		leafRenderer.setSelected(node.isSelected());
		}
		}
		returnValue = leafRenderer;
		} else {
		returnValue = nonLeafRenderer.getTreeCellRendererComponent(tree,
		value, selected, expanded, leaf, row, hasFocus);
		}
		return returnValue;*/
	}
}
